<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- tree -->
        <el-tree
          :data="list"
          :default-expand-all="true"
        >
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
          <template slot-scope="scope">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <svg-icon :icon-class="getsvgiconName(scope)" /><span>{{ scope.data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ scope.data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(scope.data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEait(scope.data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="!scope.data.children" @click.native="hDel(scope.data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>

        <el-dialog
          :title="isEdit ? '编辑部门' :'添加部门'"
          :visible.sync="showDialog"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          @close="coslefromresct"
        >
          <AddorEdit ref="deptDialog" :hhh="originList" :pid="curid" :isedit="isEdit" @success="guanbi" @close="hclose" />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>
<script>
import AddorEdit from './deptDialog.vue'
import { getDepartments, delDepartment } from '@/api/departments.js'
import { toTree } from '@/utils/tranListToTreeData'
export default {
  components: { AddorEdit },
  data() {
    return {
      curid: '',
      list: [],
      originList: [],
      showDialog: false, // 是否显示弹层
      isEdit: false // 是否处于编辑状态
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // svg名字
    getsvgiconName(scope) {
      console.log(scope)
      if (!scope.data.children) {
        return 'people'
      } else {
        if (scope.node.expanded) {
          return 'money'
        } else {
          return 'lock'
        }
      }
    },
    coslefromresct() {
      this.$refs.deptDialog.coslefromresct()
    },
    // 获取列表数据
    async loadDepartments() {
      try {
        const res = await getDepartments()
        // console.log(res)
        res.data.depts.shift()
        this.originList = res.data.depts.map(item => {
          return {
            code: item.code,
            id: item.id,
            pid: item.pid,
            name: item.name
          }
        })
        this.list = toTree(res.data.depts)
      } catch (err) {
        console.log(err)
      }
    },
    // 点击添加显示窗口
    hAdd(id) {
      this.showDialog = true
      this.curid = id
      this.isEdit = false
    },
    // 子传父
    guanbi() {
      this.showDialog = false
      this.loadDepartments()
    },
    // 点击编辑显示
    hEait(id) {
      this.showDialog = true
      this.curid = id
      this.isEdit = true

      this.$nextTick(() => {
        this.$refs.deptDialog.loadDetail()
      })
    },
    // 用户点击删除
    async doDel(id) {
      try {
        const res = await delDepartment(id)
        console.log(res)
        // 1. 提示
        this.$message({ type: 'success', message: '删除成功!' })
        // 2. 更新数据
        this.loadDepartments()
      } catch (err) {
        console.log(err)
        this.$message({ type: 'error', message: '删除失败' })
      }
    },
    hDel(id) {
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 做删除
        this.doDel(id)
      }).catch(() => {

      })
    },
    // 用户点击取消
    hclose() {
      this.showDialog = false
    }
  }

}
</script>

<style scope>
  .el-tree-node__expand-icon {
    display: none;
  }
</style>
